//
//  LOLSkillsViewController.m
//  LOLXXbox
//
//  Created by dllo on 16/1/30.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLSkillsViewController.h"
#import "LOLSkillsCollectionCell.h"
#import "LSZNetWorkTools.h"
#import "LOLSkillsModel.h"
#import "LOLSkillDetailViewController.h"
@interface LOLSkillsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation LOLSkillsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"召唤师技能";
    [self createCollec];
    [self getData];
}

- (void)createCollec{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 25)/ 4, (self.view.frame.size.width - 25)/ 4 + 25);
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[LOLSkillsCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
}

- (void)getData{
    [LSZNetWorkTools getWithUrl:@"http://lolbox.duowan.com/phone/apiSumAbility.php" parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSArray *array = result;
        for (NSDictionary *dic in array) {
            LOLSkillsModel *model = [[LOLSkillsModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    } fail:^(NSError *error) {
        
    }];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LOLSkillsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.skillModel = self.dataSource[indexPath.row];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LOLSkillDetailViewController *skillDetail = [[LOLSkillDetailViewController alloc] init];
    LOLSkillsModel *model = self.dataSource[indexPath.item];
    skillDetail.skillModel = model;
    [self.navigationController pushViewController:skillDetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
